package com.yrk.dao.impl;

import com.yrk.dao.EmpDao;
import com.yrk.entity.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Component;


import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Component("empDao")
public class EmpDaoImpl implements EmpDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public static final RowMapper<Emp> rowMapper = new ParameterizedRowMapper<Emp>() {
        @Override
        public Emp mapRow(ResultSet resultSet, int i) throws SQLException {
            Emp emp = new Emp();
            emp.setEname(resultSet.getString("ename"));
            emp.setEmpno(resultSet.getInt("empno"));
            emp.setJob(resultSet.getString("job"));
            emp.setSal(resultSet.getDouble("sal"));
            return emp;
        }
    };
//    @Override
//    public List<Emp> getEmpAll() {
//        Connection conn = null;
//        PreparedStatement ps = null;
//        ResultSet rs = null;
//       List<Emp> list = new ArrayList<Emp>();
//        try{
//            conn= dataSource.getConnection();
//            ps = conn.prepareStatement("select * from emp");
//            rs = ps.executeQuery();
//            while(rs.next()){
//                Emp emp = new Emp();
//                emp.setEname(rs.getString("ename"));
//                emp.setJob(rs.getString("job"));
//                emp.setSal(rs.getDouble("sal"));
//                list.add(emp);
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//            try {
//                rs.close();
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
//            try {
//                ps.close();
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
//            try {
//                conn.close();
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
//        }
//        return list;
//    }

    @Override
    public List<Emp> getEmpAll() {
        return jdbcTemplate.query("select * from emp", rowMapper);
    }

    @Override
    public List<Emp> getEmpByName(String empName) {
        String sql = "select * from emp where ename like ?";
        Object[] arr = {empName};
        return jdbcTemplate.query(sql, rowMapper, arr);
    }

    @Override
    public int insertEmp(Emp emp) {
        String sql = "insert into emp(ename,job,sal) values(?,?,?)";
        Object[] arr = {emp.getEname(), emp.getJob(), emp.getSal()};
        return jdbcTemplate.update(sql, arr);
    }
}
